# ======================================================================
# Project:    Closed borders, closed minds? COVID-related border closures,
#             EU support and hostility towards immigrants
#
# Script:     Figure 2
#
# Authors:    Lisa Herbig (l.j.herbig@uva.nl)
#             Asli Unan (a.unan@uva.nl)
#
# Date:       [Insert date of last edit, e.g., 2025-03-24]
# ======================================================================

# Description: This script generates Figure 2 illustrating the treatment status per week-region

# ----------------------------------------------------------------------
# Load libraries
# ----------------------------------------------------------------------
library("did2s")
library("dplyr")
library("foreign")
library("ggplot2")
library("haven")
library("lmtest")
library("lubridate")
library("modelsummary")
library("PanelMatch")
library("panelView")
library("readr")       
library("tidyr")
library("tidyverse")


## Read the files in
df_iso_work_dd = readRDS("~/work/November_2024/R_out/df_iso_work_dd_25.rds")

################################################PANEL VIEW#######################################################
#################################################################################################################

#################################################################################################################
############################################### 1. SUBSET DF TO YEAR 2020 #######################################
#################################################################################################################
SMALL_df_iso_work_dd <- subset(df_iso_work_dd, df_iso_work_dd$syear > 2019)

#################################################################################################################
############################################### 2. ADD COUNTRY NAMES ############################################
#################################################################################################################
SMALL_df_iso_work_dd$iso2_name = NA
SMALL_df_iso_work_dd$iso2_name[SMALL_df_iso_work_dd$iso2.y==1]="Austria"
SMALL_df_iso_work_dd$iso2_name[SMALL_df_iso_work_dd$iso2.y==2]="Belgium"
SMALL_df_iso_work_dd$iso2_name[SMALL_df_iso_work_dd$iso2.y==3]="Switzerland"
SMALL_df_iso_work_dd$iso2_name[SMALL_df_iso_work_dd$iso2.y==4]="Czech Republic"
SMALL_df_iso_work_dd$iso2_name[SMALL_df_iso_work_dd$iso2.y==5]="Germany"
SMALL_df_iso_work_dd$iso2_name[SMALL_df_iso_work_dd$iso2.y==6]="Denmark"
SMALL_df_iso_work_dd$iso2_name[SMALL_df_iso_work_dd$iso2.y==7]="France"
SMALL_df_iso_work_dd$iso2_name[SMALL_df_iso_work_dd$iso2.y==8]="Luxemburg"
SMALL_df_iso_work_dd$iso2_name[SMALL_df_iso_work_dd$iso2.y==9]="Netherlands"
SMALL_df_iso_work_dd$iso2_name[SMALL_df_iso_work_dd$iso2.y==10]="Poland"

#################################################################################################################
############################################### 3. CREATE NEW WEEK VARIABLE #####################################
#################################################################################################################
SMALL_df_iso_work_dd[c("YearNEW", "WeekNEW")] <- str_split_fixed(SMALL_df_iso_work_dd$weekyear, "-", 2)
#################################################################################################################
############################################### 4. START PANEL VIEW #############################################
#################################################################################################################
############################################### OUTCOME: CON_EUROPE #####################################
#################################################################################################################
################################################CORE REGIONS = 0'################################################
#################################################################################################################


# FIGURE 2 - panel view any closures
output_folder = "~/work/November_2024/R_out/figures_panelview/"

ggpanel1 <- panelview(con_europe_n ~ any_closure_binary_control0, data = SMALL_df_iso_work_dd,  index = c("iso2_name","WeekNEW"),
                      pre.post = TRUE,by.timing = TRUE, main = "Border Closure", xlab = "Week", ylab = "Border region", 
                      legend.labs = c("No border closure", "Border closure", "Missing"),
                      color = c("gray70", "#377EB8", "antiquewhite2"), background = "white", cex.main = 20, cex.axis = 12, cex.lab = 12, cex.legend = 12)

ggsave(path = "~/work/November_2024/R_out/figures_panelview/", filename = "Con-Europe_Any_Control0_25.pdf")



# Panel view BIVAR
# panel view any closures Core = 0

ggpanel1 <- panelview(con_europe_n ~ any_closure_binary_control0, data = SMALL_df_iso_work_dd,  index = c("iso2_name","WeekNEW"),
                      pre.post = TRUE,by.timing = TRUE, main = "Any Border Closure", ylab= "Connection Europe", xlab="Treatment",
                      legend.labs = c("No border closure", "Border closure", "Missing"), background = "grey", cex.main = 20, cex.axis = 8, cex.lab = 12, cex.legend = 12,
                      type = "bivar", by.unit = TRUE)
ggsave(path = "~/work/November_2024/R_out/figures_panelview/", filename = "EU_Any_Control0_BIVAR_25.pdf")

ggpanel1 <- panelview(AfD_pn ~ any_closure_binary_control0, data = SMALL_df_iso_work_dd,  index = c("iso2_name","WeekNEW"),
                      pre.post = TRUE,by.timing = TRUE, main = "Any Border Closure", ylab= "AfD", xlab="Treatment",
                      legend.labs = c("No border closure", "Border closure", "Missing"), background = "grey", cex.main = 20, cex.axis = 8, cex.lab = 12, cex.legend = 12,
                      type = "bivar", by.unit = TRUE)
ggsave(path = "~/work/November_2024/R_out/figures_panelview/", filename = "AfD_Any_Control0_BIVAR_25.pdf")

ggpanel1 <- panelview(immigration_pca_n ~ any_closure_binary_control0, data = SMALL_df_iso_work_dd,  index = c("iso2_name","WeekNEW"),
                      pre.post = TRUE,by.timing = TRUE, main = "Any Border Closure", ylab= "Attitudes Refugees", xlab="Treatment",
                      legend.labs = c("No border closure", "Border closure", "Missing"), background = "grey", cex.main = 20, cex.axis = 8, cex.lab = 12, cex.legend = 12,
                      type = "bivar", by.unit = TRUE)

ggsave(path = "~/work/November_2024/R_out/figures_panelview/", filename = "REFUGEES_Any_Control0_BIVAR_25.pdf")